<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Responsive Admin Dashboard Template">
        <meta name="keywords" content="admin,dashboard">
        <meta name="author" content="skcats">
		<title></title>
		<link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all" />
        <link th:href="@{/assets/plugins/bootstrap/css/bootstrap.min.css}" rel="stylesheet">
        <link th:href="@{/assets/plugins/font-awesome/css/font-awesome.min.css}" rel="stylesheet">
        <link th:href="@{/assets/css/custom.css}" rel="stylesheet">
	</head>
	<style>
    .layui-table-page {
        text-align: center;
    }

    .utable .layui-laypage .layui-laypage-curr .layui-laypage-em {
        background-color: #1E9FFF;
    }
    .layui-table-view thead th {
        text-align: center;
        font-weight: bold;
    }
    .layui-table-view tbody td {
        text-align: center;
    }
	</style>
<body>
<!--数据表格样式及附件-->
<script type="text/html" id="bar">
    <a id="edit" class="layui-btn layui-btn-xs" lay-event="edit">
        <i class="layui-icon">&#xe642;</i><span class="falg">ON</span> 编辑
    </a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
        <i class="layui-icon">&#xe640;</i>丢弃
    </a>
</script>
<!--搜索栏-->
<script type="text/html" id="toolbar">

</script>

<div style="margin-left: 25%" class="layui-form-item">
    <div class="layui-inline">
        <button id="ck" type="button" style="margin-top: 4px" class="layui-btn layui-btn-sm layui-btn-normal"><i class="fa fa-spin fa-refresh"></i> 查找</button>
        <button id="add" type="button" style="margin-top: 4px" class="layui-btn layui-btn-sm"><i class="fa fa-plus"></i> 添加</button>
        <div class="layui-input-inline">
            <input type="text" style="border-radius:13px" name="coursetypename" placeholder="输入课程类别查找" id="mh" autocomplete="off" class="layui-input">
        </div>
    </div>
</div>

<!--数据表格  数据表格使用的是方法渲染-->
<div class="utable">
<table class="layui-table"
       lay-data="{width:1150,height:500, url:'/coursetype/coursetypedata', page:true, id:'tab'}"
       lay-filter="tab">
    <thead>
    <tr>
        <th lay-data="{field:'coursetypeid', width:80, sort: true}">编号</th>
        <th lay-data="{field:'coursetypename'}">课程类别名称</th>
        <th lay-data="{field:'remark'}">说明</th>
        <th lay-data="{fixed: 'right', title:'操作', toolbar: '#bar', width:252}"></th>
    </tr>
    </thead>
</table>
</div>
</body>
<script th:src="@{/js/register/jquery-3.1.0.min.js}"></script>
<script th:src="@{/layui/layui.js}"></script>
<script type="text/javascript" th:inline="none">

    layui.use('table', function () {
        var table = layui.table;

        //模糊
        $("#ck").on('click',function(){
            var coursetypename=$("#mh").val();
            table.reload('tab', {
                where: { //设定异步数据接口的额外参数，任意设
                    coursetypename: coursetypename,
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

            //监听单元格编辑
        table.on('edit(tab)', function(obj){
            var tr = obj.tr;
            var oldtext = $(tr).find("td[data-field='"+obj.field+"'] div").text();
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            //layer.msg(data.emp_name)
            $.ajax({
                url: '/coursetype/coursetypeupd',
                type: 'post',
                data: {
                    coursetypeid:data.coursetypeid,
                    coursetypename:data.coursetypename,
                    remark:data.remark
                },
                //data: data.field,
                dataType: 'json',
                success:function(data){
                    if(data){
                         layer.msg(JSON.stringify("添加成功"));
                    }else {
                        obj.update({coursetypename:oldtext})
                        layer.msg('添加失败,名称重复', { icon: 5, time: 2000 });
                    }
                }
            })
        });

        //点击添加iframe窗方法
        $("#add").on('click',function(){
            layer.open({
                type: 2,
                title: '课程类别新增',
                maxmin: true, //开启最大化最小化按钮
                area: ['400px', '280px'],
                content: ['/coursetype/coursetypetoadd']//这是链接你的页面地址 url
            });
        });

        //监听行工具事件
        table.on('tool(tab)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'de' +
                '' +
                '' +
                'l') {
                layer.confirm('真的删除么?', function (index) {
                    obj.del();
                    layer.close(index);
                    $.ajax({
                        url: '/coursetype/coursetypedel',
                        type: 'post',
                        data: {
                            coursetypeid:data.coursetypeid
                        },
                        dataType: 'text'
                    })
                });
            } else if (obj.event === 'edit') {
                if($(".falg").html()=="ON"){
                    // 打开可编辑状态：
                    $(".layui-table").find('td').data('edit', true);
                    $(".falg").html("YES")

                }else {
                    $(".layui-table").find('td').data('edit', false);
                    $(".falg").html("ON")
                }
            } else if(obj.event= 'data'){

            }
        })
    });
</script>
</html>